#ifndef DW_GRAFFITI_SHADER_HELPERS_H
#	define DW_GRAFFITI_SHADER_HELPERS_H

namespace dw {
	namespace graffiti {

inline std::string loadFile(const std::string& file)
{
	std::ifstream stream(file.c_str(), std::ios::in);

	if(!stream.is_open())
		return "";

	std::string Line = "";
	std::string Text = "";

	while(getline(stream, Line))
		Text += "\n" + Line;

	stream.close();

	return Text;
}

inline bool checkProgram(GLuint ProgramName)
{
	if(!ProgramName)
		return false;

	GLint Result = GL_FALSE;
	glGetProgramiv(ProgramName, GL_LINK_STATUS, &Result);

	fprintf(stdout, "Linking program\n");
	GLint InfoLogLength;
	glGetProgramiv(ProgramName, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> Buffer(std::max(InfoLogLength, int(1)));
	glGetProgramInfoLog(ProgramName, InfoLogLength, NULL, &Buffer[0]);
	fprintf(stdout, "%s\n", &Buffer[0]);

	return Result == GL_TRUE;
}

inline bool checkShader(GLuint shaderName, const char* source)
{
	if (!shaderName)
		return false;

	GLint Result = GL_FALSE;
	glGetShaderiv(shaderName, GL_COMPILE_STATUS, &Result);

	fprintf(stdout, "Compiling shader\n%s...\n", source);
	GLint InfoLogLength;
	glGetShaderiv(shaderName, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> Buffer(InfoLogLength);
	glGetShaderInfoLog(shaderName, InfoLogLength, NULL, &Buffer[0]);
	fprintf(stdout, "%s\n", &Buffer[0]);

	return Result == GL_TRUE;
}

inline GLuint createShader(GLenum type, const std::string& file)
{
	bool isValid = true;
	GLuint shaderName = 0;

	if (!file.empty())
	{
		std::string source = loadFile(file);
		const char* sourcePtr = source.c_str();

		shaderName = glCreateShader(type);
		glShaderSource(shaderName, 1, &sourcePtr, NULL);
		glCompileShader(shaderName);
		
		checkShader(shaderName, sourcePtr);
	}

	return shaderName;
}

	} // namespace graffiti
} // namespace dw

#endif // !DW_GRAFFITI_SHADER_HELPERS_H
